package com.jzl.swing.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jzl.swing.dao.SupplierDao;
import com.jzl.swing.entity.Supplier;
import com.jzl.swing.rowmapper.impl.SupplierRowMapper;
import com.jzl.swing.util.JDBCTemplate;

public class SupplierDaoImpl implements SupplierDao{

	@Override
	public int add(Supplier supplier) throws SQLException {
		String sql = "insert into suppliers(supplier_name, supplier_describ, supplier_contacter, supplier_phone, supplier_address)values(?,?,?,?,?)";
		return JDBCTemplate.executeUpdate(sql, supplier.getSupplier_name(), supplier.getsupplier_describ(), supplier.getsupplier_contacter(), supplier.getsupplier_phone(), supplier.getsupplier_address());		
	}

	@Override
	public int delete(Supplier supplier) throws SQLException {
		String sql = "delete from suppliers where supplier_id=?";
		return JDBCTemplate.executeUpdate(sql, supplier.getSupplier_id());
	}
	
	@Override
	public int modify(Supplier supplier) throws SQLException {
		String sql = "update suppliers set supplier_name=?,supplier_describ=?,supplier_contacter=?, supplier_phone=?, supplier_address=? where supplier_id=?";
		return JDBCTemplate.executeUpdate(sql, supplier.getSupplier_name(), supplier.getsupplier_describ(), supplier.getsupplier_contacter(), supplier.getsupplier_phone(), supplier.getsupplier_address(), supplier.getSupplier_id());
	}

	@Override
	public List<Supplier> queryAll() throws SQLException {
		List<Supplier> supplierList = new ArrayList<Supplier>();
		String sql = "select * from suppliers";
		List<Object> list = JDBCTemplate.executeQuery(sql, new SupplierRowMapper());
		for (Object object : list) {
			supplierList.add((Supplier) object);
		}
		return supplierList;
	}

	@Override
	public Supplier queryById(String supplier_id) throws SQLException {
		String sql = "select * from suppliers where supplier_id=?";
		List<Object> list = new ArrayList<Object>();
		list = JDBCTemplate.executeQuery(sql, new SupplierRowMapper(), supplier_id);
		if (list.size()>0) {
			return (Supplier) list.get(0);
		}
		return null;
	}

	@Override
	public List<Supplier> queryByName(String Supplier_name) throws SQLException {
		List<Supplier> supplierList = new ArrayList<Supplier>();
		String sql = "select * from suppliers where supplier_name=?";
		List<Object> list = JDBCTemplate.executeQuery(sql, new SupplierRowMapper(), Supplier_name);
		for (Object object : list) {
			supplierList.add((Supplier) object);
		}
		return supplierList;
	}

}
